package cz.data.domain.quality.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.domain.quality.mapstruct.ScheduleJobMapper;
import cz.data.domain.quality.model.entity.ScheduleJobEntity;
import cz.data.domain.quality.model.query.ScheduleJobQuery;
import cz.data.domain.quality.model.vo.ScheduleJobVo;
import cz.data.domain.quality.service.ScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"数据质量监控任务"})
@RestController
@RequestMapping("/scheduleJobs")
public class ScheduleJobController extends BaseController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    @Autowired
    private ScheduleJobMapper scheduleJobMapper;

    @ApiOperation(value = "数据质量监控任务-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ScheduleJobVo> getScheduleJobById(@PathVariable String id) {
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.getScheduleJobById(id);
        return new SimpleDataResponse<>(scheduleJobMapper.toVO(scheduleJobEntity));
    }

    @ApiOperation(value = "数据质量监控任务-分页查询")
    @ApiImplicitParam(name = "scheduleJobQuery", value = "查询实体scheduleJobQuery", required = true, dataTypeClass = ScheduleJobQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ScheduleJobVo>> getScheduleJobPage(ScheduleJobQuery scheduleJobQuery) {
        QueryWrapper<ScheduleJobEntity> queryWrapper = new QueryWrapper<>();
        IPage<ScheduleJobEntity> page = scheduleJobService.page(new Page<>(scheduleJobQuery.getPageNum(), scheduleJobQuery.getPageSize()), queryWrapper);
        List<ScheduleJobVo> collect = page.getRecords().stream().map(scheduleJobMapper::toVO).collect(Collectors.toList());
        JsonPage<ScheduleJobVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "数据质量监控任务-暂停任务")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @PostMapping("/pause/{id}")
    public BaseResponse pauseScheduleJobById(@PathVariable("id") String id) {
        scheduleJobService.pauseScheduleJobById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "数据质量监控任务-恢复任务")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @PostMapping("/resume/{id}")
    public BaseResponse resumeScheduleJobById(@PathVariable("id") String id) {
        scheduleJobService.resumeScheduleJobById(id);
        return BaseResponse.success();
    }
}
